package main

import (
	"fmt"
	"time"
	RxGo "github.com/myrxgo/rxgo"
)

func main() {
	fmt.Print("Test Distinct\n")
	RxGo.Just(11,12,13,14,15,16,17).Distinct().Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Print("Test ElementAt\n")
	RxGo.Just(11,12,13,14,15,16,17).ElementAt(3).Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Print("Test First\n")
	RxGo.Just(11,12,13,14,15,16,17).First().Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Print("Test IgnoreElements\n")
	RxGo.Just(11,12,13,14,15,16,17).IgnoreElements().Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Print("Test Last\n")
	RxGo.Just(33, 1, 0, 215, 4, 6).Last().Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Print("Test Skip\n")
	RxGo.Just(11,12,13,14,15,16,17).Skip(3).Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Print("Test SkipLast\n")
	RxGo.Just(11,12,13,14,15,16,17).SkipLast(2).Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Print("Test Take\n")
	RxGo.Just(11,12,13,14,15,16,17).Take(3).Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Print("Test TakeLast\n")
	RxGo.Just(11,12,13,14,15,16,17).TakeLast(3).Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()

	observableP := make(chan interface{})
	go func() {
		RxGo.Just(1, 2, 3, 4, 5).Map(func(x int) int {
			switch x {
			case 1:
				time.Sleep(3 * time.Millisecond)
			case 2:
				time.Sleep(1 * time.Millisecond)
			case 3:
				time.Sleep(2 * time.Millisecond)
			case 4:
				time.Sleep(2 * time.Millisecond)
			default:
				time.Sleep(1 * time.Millisecond)
			}
			return x
		}).Subscribe(func(x int) {
			observableP <- x
		})
	}()
	fmt.Print("Test Sample\n")
	RxGo.Just(1, 2, 3, 4, 5).Map(func(x int) int {
		time.Sleep(2*time.Millisecond)
		return x
	}).Sample(observableP).Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Print("Test Debounce\n")
	RxGo.Just(0, 1, 1, 3, 1, 5).Map(func(x int) int {
		switch x {
		case 0:
			time.Sleep(0 * time.Millisecond)
		case 1:
			time.Sleep(1 * time.Millisecond)
		case 3:
			time.Sleep(2 * time.Millisecond)
		case 5:
			time.Sleep(3 * time.Millisecond)
		default:
			time.Sleep(1 * time.Millisecond)
		}
		return x
	}).Debounce(2 * time.Millisecond).Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()

}